# -*- coding: utf-8 -*-
import cStringIO
import ho.pisa as pisa
import os
from reportlab.pdfbase import pdfmetrics  
from reportlab.pdfbase.ttfonts import TTFont  
pdfmetrics.registerFont(TTFont('msyh', r'c:\windows\fonts\msyh.ttf'))  
from sx.pisa3 import pisa_default  
pisa_default.DEFAULT_FONT["helvetica"]="msyh" 

pisa.showLogging()

def HTML2PDF(data, filename, open=False):


    pdf = pisa.CreatePDF(
        cStringIO.StringIO(data),
        file(filename, "wb"))

    if open and (not pdf.err):
        os.startfile(str(filename))

    return not pdf.err

if __name__=="__main__":
    HTMLTEST = """
    <html><body>
    <p>Hello <strong style="color: #f00;">World</strong>
    <hr>
    <table border="1" style="background: #eee; padding: 0.5em;">
        <tr>
            <td>Amount</td>
            <td>Description</td>
            <td>Total</td>
        </tr>
        <tr>
            <td>1</td>
            <td>Good weather</td>
            <td>0 EUR</td>
        </tr>
        <tr style="font-weight: bold">
            <td colspan="2" align="right">Sum</td>
            <td>0 EUR</td>
        </tr>
    </table>
    </body></html>
    """
    # for x in (1,2,3):
    x=1
    f=open('310420120549193168.%d.html'%x).read()
    HTML2PDF(f, "310420120549193168.%d.pdf"%x, open=True)
